package com.demo.ruiji.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.ruiji.common.BaseContext;
import com.demo.ruiji.common.R;
import com.demo.ruiji.dto.OrdersDto;
import com.demo.ruiji.pojo.OrderDetail;
import com.demo.ruiji.pojo.Orders;
import com.demo.ruiji.service.OrderDetailService;
import com.demo.ruiji.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * 订单表 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;

    //提交订单
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        //比较繁琐，在service实现
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 后台分页查询订单
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number, String beginTime, String endTime) {
        //分页构造器
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件,根据订单时间进行排序
        lambdaQueryWrapper.orderByDesc(Orders::getOrderTime);

        //打印订单号
        log.info("订单号：{}",number);

        //如果订单号不为空
        if(number != null){
            //添加订单号比较条件
            lambdaQueryWrapper.eq(Orders::getNumber,number);
        }

        //如果 开始时间 和 结束时间 都不为空
        if(beginTime != null && endTime != null){
            //SQL : where (orderTime BETWEEN beginTime AND endTime)
            //添加范围内查询条件 筛选出下单时间 大于等于 开始时间 小于等于 结束时间
            lambdaQueryWrapper.between(Orders::getOrderTime,beginTime,endTime);
        }

        //进行分页查询
        ordersService.page(pageInfo, lambdaQueryWrapper);

        return R.success(pageInfo);
    }



    /**
     *  更改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> status(@RequestBody Orders orders) {
        log.info("订单状态：{}", orders.toString());
        ordersService.updateById(orders);

        return R.success("派送成功");
    }


    /**
     * 用户订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> userPage(int page , int pageSize) {

        //分页构造器
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();

        //SQL：select * from orders where user_id = ? order by order_time desc

        //条件构造器
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加用户ID
        lambdaQueryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        //添加排序条件,根据订单时间进行倒序
        lambdaQueryWrapper.orderByDesc(Orders::getOrderTime);

        //进行分页查询
        ordersService.page(pageInfo,lambdaQueryWrapper);

        //对象拷贝 将pageInfo 拷贝 到 orderDtoPage 忽略 records
        BeanUtils.copyProperties(pageInfo,ordersDtoPage,"records");

        //获取pageInfo的records
        List<Orders> records = pageInfo.getRecords();

        //遍历records
        List<OrdersDto> list = records.stream().map((item) -> {

            OrdersDto ordersDto = new OrdersDto();
            //将 records的数据拷贝到 ordersDto
            BeanUtils.copyProperties(item,ordersDto);

            //SQL：select * from order_detail where order_id = ?

            //条件构造器
            LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
            //通过records里面的订单ID [number] 对比 订单明细表的order_id 查询订单明细
            queryWrapper.eq(OrderDetail::getOrderId,item.getNumber());
            //查询出订单明细信息 列表
            List<OrderDetail> orderDetail = orderDetailService.list(queryWrapper);

            ordersDto.setOrderDetails(orderDetail);
            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(list);

        return R.success(ordersDtoPage);
    }
}
